--
-- Created by IntelliJ IDEA.
-- User: SOSO
-- Date: 14-11-5
-- Time: 下午3:29
-- 一些到多出用到的公共方法
--

require "support/serial"


PublicFunction = {}
PublicFunction.__index = PublicFunction


function windowSize()
    return cc.Director:getInstance():getWinSize()
end




-----------------------------------------------------------------------------
--  简写的序列化和打印
-----------------------------------------------------------------------------
function SSLookTable(nBlank,thing)
    local blankStr = ""
    for i=1,nBlank do  blankStr=blankStr.."  "  end        --   string.format("%s  ",blankStr)end
    -------------------------------------
    local resStr = ""
    for k,v in pairs(thing)do 
        resStr = resStr.."\n"..blankStr.."["..k.."]".."=["..SSLookOther(nBlank+1,v).."]"
        -- string.format("%s\n%s[%s]=[%s]",resStr,blankStr,""..k,SSLookOther(nBlank+1,v))
    end
    return resStr
end


function SSLookOther(nBlank,thing)
    if thing==nil then
        return "(==nil)"
    elseif type(thing)=="table" then
        return SSLookTable(nBlank+1,thing)
    elseif type(thing)=="number" then
        return "(数字:"..thing..")"
        -- string.format("(数字:%s)",""..thing)
    elseif type(thing)=="boolean" then
        if thing then return "(true)"
        else return "(false)" end
    elseif type(thing)=="string" then
        return "("..thing..")"
        -- string.format("(%s)",thing)
    else
        return "(无法解析的类型:"..type(thing)
        -- string.format("(无法解析 tab的类型:%s)",type(thing))
    end
end


function showTable(tab)
    if type(tab)~="table" then
        return trace(tab)
    end
    --------------------
    return SSLookTable(0,tab)
end


function trace(tab)
    if tab==nil then
        return "(==nil)"
    elseif type(tab)=="table" then
        return showTable(tab)            -- serialize(tab)
    elseif type(tab)=="number" then
        return string.format("(数字:%s)",""..tab)
    elseif type(tab)=="boolean" then
        if tab then return "(true)"
        else return "(false)" end
    elseif type(tab)=="string" then
        return string.format("(%s)",tab)
    else
        return string.format("\n(无法解析 tab的类型:%s)\n",type(tab))
    end
end




function cclog(...)
    print(string.format(...))
end



function log(...)
    print(string.format(...))
end



function cclook(...)
    -- if true then return end
    local args = {...}
    local ars = {}
    -- 这里用写死的范围是因为：如果传入参数有一个为nil，数组取的就不完整，
    -- 下面 format 需要string， 遇到 nil 就会报错。
    for i=2,10 do  
        ars[i-1] = trace(args[i])
    end
    cclog(args[1], ars[1],ars[2],ars[3],ars[4],ars[5],ars[6],ars[7],ars[8],ars[9],ars[10])
end












-----------------------------------------------------------------------------
-- 将秒数转化为时间格式 00：00：00
-----------------------------------------------------------------------------
function PublicFunction.getTimeStringForSecond(second)
    local hour =(second-second%3600)/3600
    local min =(second%3600-second%60)/60
    local sec = second%60
    return string.format("%02d:%02d:%02d",hour,min,sec)
end
-----------------------------------------------------------------------------
-- 将秒数转化为时间格式 00：00
-----------------------------------------------------------------------------
function PublicFunction.getTimeStringForMinute(second)
    --local hour =(second-second%3600)/3600
    local min =(second%3600-second%60)/60
    local sec = second%60
    return string.format("%02d:%02d", min,sec)
end




-----------------------------------------------------------------------------
-- 获取某个表的数量，过滤 nil 的项
-----------------------------------------------------------------------------
function getTableCount(tab)
    local count = 0
    for k,v in pairs(tab)do 
        if v then count = count+1 end
    end
    return count
end





function distance(p1,p2)
    local delta_x = p1.x - p2.x
    local delta_y = p1.y - p2.y
    return math.sqrt(delta_x * delta_x + delta_y * delta_y)
end




function getAngle( poi_o , poi_r )
    if(poi_o.x==poi_r.x and poi_r.y>=poi_o.y ) then 
        return 90
    elseif(poi_o.x==poi_r.x and poi_r.y<poi_o.y ) then 
        return 270
    end

    local ang = math.atan( (poi_r.y - poi_o.y)/(poi_r.x - poi_o.x) ) * 180/ math.pi
    if( poi_r.y>=poi_o.y ) then 
        return (ang+180)%180
    else 
        return (ang+180)%180 + 180
    end 
    -- local ang = (poi_r.y - poi_o.y)/(poi_r.x - poi_o.x )*180/3.14159265
    -- ang = (ang%360 + 360)%360
    -- return ang
end





-----------------------------------------------------------------------------
-- 给一个非全屏的
-----------------------------------------------------------------------------
-- 判断世界坐标系的点worldPoint是否处于node内部。
local function isContain(node,worldPoint)
    local ptO = node:convertToNodeSpace(cc.p(0,0))
    local recBox = node:getBoundingBox()
    local ptTou = node:convertToNodeSpace(worldPoint)
    recBox.x = 0
    recBox.y = 0
    return cc.rectContainsPoint(recBox,ptTou)
end

-- 添加触摸遮罩
local touchMaskId = 1
function addMaskOnNodeParentWithSameSize(node,inSideFunc,outSideFunc)
    nOpacity = nOpacity or 0
    
    local nodeMask = cc.LayerColor:create()
    local rect = node:getBoundingBox()
    nodeMask:setAnchorPoint(cc.p(0.5,0.5))
    nodeMask:setColor(cc.c3b(0,0,0))
    nodeMask:setOpacity(150)---- nOpacity
    nodeMask:setContentSize(cc.size(rect.width,rect.height))
    
    local px,py = node:getPosition()
    nodeMask:setPosition(cc.p(px,py))
    node:getParent():addChild(nodeMask,1000)

---------------   
    -- 菊花:
    local runSp = cc.Sprite:createWithSpriteFrameName("res/public/img_load.png")
    runSp:setAnchorPoint(cc.p(0.5,0.5))
    runSp:setPosition(cc.p(rect.width/2,rect.height/2))
    nodeMask:addChild(runSp,1001)
    local rotateAct = cc.RotateBy:create(0.2,30)
    local repeatAct = cc.RepeatForever:create(rotateAct)
    runSp:runAction(repeatAct)
----------------- 
    nodeMask:ignoreAnchorPointForPosition(false)
    nodeMask:setName("TouchMask_"..touchMaskId)
    touchMaskId = touchMaskId + 1

    local isTouchBeganOutside = false
    local function onTouchBegan(touch,event)
        if event:getCurrentTarget():isVisible()and 
            isContain(nodeMask,touch:getLocation())then
            return true
        end
        return false
    end

    local function onTouchMoved(touch,event)
        return false
    end

    local function onTouchEnded(touch,event)
        if event:getCurrentTarget():isVisible()and isContain(nodeMask,touch:getLocation())then   
            if inSideFunc~=nil then inSideFunc()end 
        end
        return false
    end
    
    local listener = cc.EventListenerTouchOneByOne:create() 
    listener:setSwallowTouches(true);
    listener:registerScriptHandler(onTouchBegan,cc.Handler.EVENT_TOUCH_BEGAN) 
    listener:registerScriptHandler(onTouchMoved,cc.Handler.EVENT_TOUCH_MOVED)
    listener:registerScriptHandler(onTouchEnded,cc.Handler.EVENT_TOUCH_ENDED)
    nodeMask:getEventDispatcher():addEventListenerWithSceneGraphPriority(listener,nodeMask) 
    return nodeMask
end



function removeMaskSlowly(node)
    local act = cc.FadeOut:create(0.2)
    local function evt() node:removeFromParent()end
    local cback = cc.CallFunc:create(evt)
    local seq = cc.Sequence:create(act,cback)
    node:runAction(seq)
end






















